<template>
  <div class="history-box" v-if="storyList.length">
    <div class="title">
      <span>搜索历史</span>
      <van-icon name="delete-o" @click="handleClearHistory" />
    </div>
    <ul class="history-list">
      <li class="history-item" v-for="(item, index) in storyList" :key="index" @click="searchMusic(item)">
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import useHistoryStore from "@/store/historyStore";
import { toRefs } from "vue";

const emit = defineEmits<{
  (e: "searchMusic", value: string): void;
}>();
const { storyList, clearHistory } = toRefs(useHistoryStore());

const handleClearHistory = () => {
  clearHistory.value();
};

const searchMusic = (value: string) => {
  emit("searchMusic", value);
};
</script>

<style scoped lang="scss">
.history-box {
  width: 100%;
  box-sizing: border-box;
  padding: 15px;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
    font-size: 18px;
  }

  .history {
    &-list {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 10px;
    }
    &-item {
      padding: 6px 10px;
      background: hsla(0, 0%, 100%, 0.1);
      border-radius: 14px;
      font-size: 14px;
      height: 12px;
      line-height: 12px;
    }
  }
}
</style>
